package org.example.controller.backup;

import cn.hutool.core.bean.BeanUtil;
import org.example.common.result.ResponseData;
import org.example.flowable.handler.HistTaskQueryHandler;
import org.example.flowable.handler.TaskHandler;
import org.example.flowable.handler.TaskQueryHandler;
import org.example.repository.model.HistTaskVO;
import org.example.repository.model.TaskVO;
import org.flowable.engine.HistoryService;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 流程任务相关
 */
@RestController
@RequestMapping("api/flow/query")
public class QueryController {

    protected static Logger logger = LoggerFactory.getLogger(QueryController.class);
    @Autowired
    private TaskHandler taskHandler;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private TaskQueryHandler taskQueryHandler;
    @Autowired
    private HistTaskQueryHandler histTaskQueryHandler;

    @RequestMapping(value = "/task/taskId ", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData queryTask(String taskId) {
        Task task = taskQueryHandler.taskId(taskId);
        TaskVO taskVO = BeanUtil.copyProperties(task, TaskVO.class);
        return ResponseData.success(taskVO);
    }

    @RequestMapping(value = "/task/list/userId ", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData queryUserList(String userId) {
        List<Task> tasks = taskQueryHandler.taskCandidateOrAssigned(userId);
        List<TaskVO> list = new ArrayList<>();
        tasks.forEach(item->{
            list.add(BeanUtil.copyProperties(item,TaskVO.class));
        });

        return ResponseData.success(list);
    }




    @RequestMapping(value = "/comment ", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData getTaskComments(String taskId) throws Exception {
        List<Comment> taskComments = taskHandler.getTaskComments(taskId);
        return ResponseData.success(taskComments);
    }


    @RequestMapping(value = "/task/list/instanceId", method = RequestMethod.GET)
    @ResponseBody
    public Object listByInstanceId(String instanceId) {
        List<HistoricTaskInstance> list = histTaskQueryHandler.listByInstanceId(instanceId);
        List<HistTaskVO> copyList = new ArrayList<>();
        list.forEach(item->{
            copyList.add(BeanUtil.copyProperties(item, HistTaskVO.class));
        });

        return ResponseData.success(copyList);
    }

    @RequestMapping(value = "/task/pageList/instanceId", method = RequestMethod.GET)
    @ResponseBody
    public Object pageListByInstanceId(String instanceId, Integer page, Integer step) {
        List<HistoricTaskInstance> list = histTaskQueryHandler.pageListByInstanceId(instanceId, page, step);
        return ResponseData.success(list);
    }


    @RequestMapping(value = "/task/list/unclaim", method = RequestMethod.GET)
    @ResponseBody
    public Object unclaim(String userId, Integer page, Integer step) {
        List<Task> taskList = taskQueryHandler.taskCandidateUser(userId, page, step);
        List<TaskVO> list = new ArrayList<>();
        taskList.forEach(item->{
            list.add(BeanUtil.copyProperties(item, TaskVO.class));
        });

        return ResponseData.success(list);
    }

    @RequestMapping(value = "/task/list/claimed", method = RequestMethod.GET)
    @ResponseBody
    public Object claimed(String userId) {
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().taskAssignee(userId).list();
        return ResponseData.success(list);
    }


    @RequestMapping(value = "/task/active", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData<HistoricTaskInstance> query(String instanceId) throws Exception {
        //单实例任务--单个活动任务
        //方式一：需要处理懒加载问题
        //Task task = taskQueryHandler.processInstanceId(instanceId);
        //TaskVO taskVO = new TaskVO();
        //BeanUtils.copyProperties(task, taskVO);

        //方式二：正常使用，通过historyService查询当前活动任务，无需进行转换
        HistoricTaskInstance historicTaskInstance =
                historyService
                        .createHistoricTaskInstanceQuery()
                        .processInstanceId(instanceId)
                        .unfinished().singleResult();

        //多实例并行任务--多个活动任务
        //List<Task> list = taskService.createTaskQuery().processInstanceId(instanceId).active().list();

        return ResponseData.success(historicTaskInstance);
    }


}

